package com.lvcoding.jwtdemo.controller;

import com.lvcoding.jwtdemo.util.JwtUtil;
import com.lvcoding.jwtdemo.util.Res;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("test")
@RestController
public class TestController {


    @GetMapping("index")
    public Res index(HttpServletRequest request) {

        String username = (String) request.getAttribute("username");
        String age = (String) request.getAttribute("age");

        Map<String, String> map = new HashMap<>();
        map.put("username", username);
        map.put("age", age);

        return Res.success(map);
    }

    @GetMapping("login")
    public Res login(String username) {
        Map<String, String> map = new HashMap<>();
        map.put("username", username);
        map.put("age", "25");
        String token = JwtUtil.createToken(map);
        return Res.success(token);
    }
}
